import axios, { CancelToken } from 'axios'
import yblog from '../config/yblog'
import { message } from 'antd';
import Cookies from 'js-cookie'
import { CheckFileSize } from '../utils/tool/file'

export type CancelFunc = (func: Function) => void

const CancelTokenModel = axios.CancelToken;

const cancelTokenInstance = (cancelFunc?: CancelFunc): CancelToken | undefined => {
  if (cancelFunc) {
    const source = CancelTokenModel.source();
    cancelFunc(source.cancel)
    return source.token
  }
}

// 创建axios实例
const service = axios.create({
  timeout: 10000, // 请求超时时间
})

// request拦截器
service.interceptors.request.use(
  (config) => {
    yblog('================= url ==================')
    yblog(config.url)
    yblog('================= 参数 ==================')
    yblog(config.method === 'get' ? config.params : config.data)
    return config
  },
  (error) => {
    Promise.reject(error)
  },
)

// respone拦截器
service.interceptors.response.use(
  (response) => {
    const res = response.data
    yblog('================= response ==================', response.config.url)
    yblog(res)
    if (res.code !== 0) {
      message.error(res.msg ? res.msg : '网络错误');
      // if (res.code === 13001 || res.code === 13002) {
      //   WPopView.msgError('请重新登录')
      //   WPopView.store.dispatch('FED_LOG_OUT')
      //   WPopView.router.push('/')
      // }
      return Promise.reject(res)
    } else {
      return res.data
    }
  },
  (error) => {
    message.error(error.message);
    return Promise.reject(error)
  },
)

// 自定义封装git
export function get(url: string, params = {}, cancelFunc?: CancelFunc) {
  return service.get(url, {
    // 请求参数
    params,
    // 取消的Token
    cancelToken: cancelTokenInstance(cancelFunc)
  })
}

export function deleteAjax(url: string, id: string, params = {}, cancelFunc?: CancelFunc) {
  return service.delete(`${url}${id}`, {
    // 请求参数
    params,
    // 取消的Token
    cancelToken: cancelTokenInstance(cancelFunc)
  })
}

// 自定义封装POST
export function post(url: string, params = {}, cancelFunc?: CancelFunc) {
  return service.post(url, params, {
    headers: {
      'x-csrf-token': Cookies.get('csrfToken')
    },
    // 取消的Token
    cancelToken: cancelTokenInstance(cancelFunc)
  })
}

// 图片上传
export function upImage(url: string, name: string, file: File, cancelFunc?: CancelFunc) {
  if (!CheckFileSize(file, 2 * 1024)) {
    message.error('图片大小不能超2M')
    return Promise.reject({
      msg: '图片大小不能超2M',
    })
  }
  // 创建form对象
  const params = new FormData()
  // 通过append向form对象添加数据
  params.append(name, file, file.name)
  return service.post(url, params, {
    headers: {
      'Content-Type': 'multipart/form-data',
      'x-csrf-token': Cookies.get('csrfToken')
    },
    // 取消的Token
    cancelToken: cancelTokenInstance(cancelFunc)
  })
}

// 上传文件
export function upFile(url: string, params = {}, cancelFunc?: CancelFunc) {
  return service.post(url, params, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    // 取消的Token
    cancelToken: cancelTokenInstance(cancelFunc)
  })
}
